﻿/*
    File name:          DLLImport
    File type:          Class
    Author:             Sune Krog-Meyer 09888
    Creation date:      21-03-2011
    Description:        Contains all DLL-imports for the robot.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using i4pro_gruppe_d.Types;


namespace i4pro_gruppe_d.DLL
{
    class DLLHandle
    {

        #region Init and Control Funktions
        
        //Home the Robot
        [DllImport("USBC.dll", EntryPoint = "?Home@@YAHEP6AXPAX@Z@Z", CallingConvention = CallingConvention.Cdecl)]
        public static extern bool Homing(Byte axis, HomeEvent homeEvent);

        //Home event
        [UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public delegate void HomeEvent(Byte homeNotif);

        //Init the Robot
        [DllImport("USBC.dll", EntryPoint = "?Initialization@@YAHFFP6AXPAX@Z1@Z", CallingConvention = CallingConvention.Cdecl)]
        public static extern bool Init(ushort mode, ushort systemtType, DataEvent dataEvent, ErrorEvent errorEvent);

        //Init Event
        [UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public delegate void DataEvent([MarshalAs(UnmanagedType.SysInt)] IntPtr dataConfig);

        //Init Error Event
        [UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public delegate void ErrorEvent(IntPtr errorInfo);

        //Enable Control
        [DllImport("USBC.dll", EntryPoint = "?Control@@YAHEH@Z", CallingConvention = CallingConvention.Cdecl)]
        public static extern bool ControlOn(byte axis, bool isOn);

        #endregion

        #region Gripper Funktions

        //Open Gripper
        [DllImport("USBC.dll", EntryPoint = "?OpenGripper@@YAHXZ", CallingConvention = CallingConvention.Cdecl)]
        public static extern bool OpenGripper();

        //Close Gripper
        [DllImport("USBC.dll", EntryPoint = "?CloseGripper@@YAHXZ", CallingConvention = CallingConvention.Cdecl)]
        public static extern bool CloseGripper();

        //Messure Width
        [DllImport("USBC.dll", EntryPoint = "?GetJaw@@YAHPAF0@Z", CallingConvention = CallingConvention.Cdecl)]
        public static extern bool GetJaw(ref short perc, ref short metric);

        #endregion

        #region Conveyer Funktions
        
        //Enter Manual Movement
        [DllImport("USBC.dll", EntryPoint = "?EnterManual@@YAHF@Z", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public static extern bool EnterManual(short moveType);

        //Close Manual Movement
        [DllImport("USBC.dll", EntryPoint = "?CloseManual@@YAHXZ", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public static extern bool CloseManual();

        //Start Manual Movement
        [DllImport("USBC.dll", EntryPoint = "?MoveManual@@YAHEJ@Z", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public static extern bool MoveManual(byte axis, int velocity);

        //Stop Movement
        [DllImport("USBC.dll", EntryPoint = "?Stop@@YAHE@Z", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public static extern bool Stop(byte axis);

        #endregion

        #region Vector Funktions
        
        //Create new Vector
        [DllImport("USBC.dll", EntryPoint = "?DefineVector@@YAHEPADF@Z", CallingConvention = CallingConvention.Cdecl)]
        public static extern bool DefineVector(byte group, [MarshalAs(UnmanagedType.LPStr)] string vectorName, short size);

        //Add point to Vector
        [DllImport("USBC.dll", EntryPoint = "?Teach@@YAHPADFPAJFJ@Z", CallingConvention = CallingConvention.Cdecl)]
        public static extern bool Teach([MarshalAs(UnmanagedType.LPStr)] string vectorName, short pointNumber, int[] coords, short coordSize, int pointType);

        //Get current Position
        [DllImport("USBC.dll", EntryPoint = "?GetCurrentPosition@@YAHPAY07J00@Z", CallingConvention = CallingConvention.Cdecl)]
        public static extern bool getPos(Vector enc, Vector joint, Vector XYZ);

        #endregion

        #region Watch Inputs
        
        //Stop Watching Digital Inputs
        [DllImport("USBC.dll", EntryPoint = "?CloseWatchDigitalInp@@YAHXZ", CallingConvention = CallingConvention.Cdecl)]
        public static extern bool StopWatchDigit();

        //Start Watching Digital Inputs
        [DllImport("USBC.dll", EntryPoint = "?WatchDigitalInp@@YAHP6AXPAX@Z@Z", CallingConvention = CallingConvention.Cdecl)]
        public static extern bool StartWatchDigital(DigitalEvent digital);

        //Digital Input Handler
        [UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public delegate void DigitalEvent(Int32 input);

        #endregion

        #region Robot Movement Funktions
        
        //Move Robotarm Linear
        [DllImport("USBC.dll", EntryPoint = "?MoveLinear@@YAHPADF0F@Z", CallingConvention = CallingConvention.Cdecl)]
        public static extern bool MoveLinear([MarshalAs(UnmanagedType.LPStr)] string vectorName, short point, [MarshalAs(UnmanagedType.LPStr)] string nextVector, short nextPoint);

        //Watch Movement Motion
        [DllImport("USBC.dll", EntryPoint = "?WatchMotion@@YAP6AXPAX@ZP6AX0@Z1@Z", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr WatchMotion(EndMotionEvent endMotion, StartMotionEvent startMotion);
        
        //Motion Ended Event
        [UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public delegate void EndMotionEvent(IntPtr end);

        //Motion Started Event
        [UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public delegate void StartMotionEvent(IntPtr start);

        #endregion

        #region Utility Funktions

        //Emergency check
        [DllImport("USBC.dll", EntryPoint = "?IsEmergency@@YAHXZ", CallingConvention = CallingConvention.Cdecl)]
        public static extern bool IsEmergency();        

        //Close Connection to the Robot
        [DllImport("USBC.dll", EntryPoint = "?CloseUSBC@@YAXXZ", CallingConvention = CallingConvention.Cdecl)]
        public static extern void closeConnection();

        //Check if the Robot is Online
        [DllImport("USBC.dll", EntryPoint = "?IsOnLineOk@@YAHXZ", CallingConvention = CallingConvention.Cdecl)]
        public static extern bool IsOnline();

        #endregion
    }
}
